package com.eshore.textanalyzer.elk.common;

/**
 * @author xyd
 * @date 2021/5/10
 */

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * @author
 * 封装一个共享使用的类数据
 * 封装一个返回的响应数据
 */
public class ResponseMessage<T> implements Serializable {
    private static final int STATUS_SUCCESS = 0; //成功
    private static final int STATUS_FAIL = 1; //失败

    /**
     * 成功与否代码
     * 成功为0 失败为1
     */
    private int status;
    /**
     * 返回响应信息
     */
    private String msg;
    /**
     * 返回对象、数据信息
     */
    private T data;

    @JsonIgnore
    private transient static Object defaultData = new Object();

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 出错的请求路径
     */
    private String path;

    /**
     * 返回成功与否代码
     * @param status 成功为0 失败为1
     */
    private ResponseMessage(int status){
        this.status=status;
    }

    /**
     * 返回数据跟判断代码
     * @param status
     * @param data
     */
    private ResponseMessage(int status,T data){
        this.status=status;
        this.data=data;
    }

    /**
     * 返回判断代码、数据、响应信息
     * @param status
     * @param msg
     * @param data
     */
    private ResponseMessage(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回判断代码、响应信息、数据、请求路径
     * @param status
     * @param msg
     * @param data
     * @param path
     */
    private ResponseMessage(int status, String msg, T data, String path) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.path = path;
    }

    /**
     * 返回判断代码、响应信息
     * @param status
     * @param msg
     */
    private ResponseMessage(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore //返回时忽略掉这个属性
    public boolean isSuccess() {
        return this.status == STATUS_SUCCESS;
    }

    public int getStatus() {
        return this.status;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {return this.msg;}

    public long getTimestamp () {
        return this.timestamp;
    }

    public String getPath() {
        return this.path;
    }


    public static <T> ResponseMessage<T> createBySuccess() {
        return new ResponseMessage<T>(Code.SUCCESS.getCode());
    }


    public static <T> ResponseMessage<T> createBySuccessMessage(String msg) {
        return new ResponseMessage<T>(Code.SUCCESS.getCode(), msg,null);
    }

    @SuppressWarnings("unchecked")
    public static <T> ResponseMessage<T> createBySuccess(T data) {
        if (data == null) {
            data = (T) defaultData;
        }
        return new ResponseMessage<T>(Code.SUCCESS.getCode(), Code.SUCCESS.getMsg(), data);
    }

    public static <T> ResponseMessage<T> createBySuccess(String msg, T data) {
        return new ResponseMessage<T>(Code.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseMessage<T> createBySuccess(BaseCode code) {
        return createBySuccessMessage(Code.SUCCESS.getMsg());
    }

    /**
     * 下面是错误的请求
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> createByError(String message) {
        return createByError(Code.ERROR, message, null);
    }

    public static <T> ResponseMessage<T> createByError(BaseCode code) {
        return createByError(code, null);
    }

    public static <T> ResponseMessage<T> createByError(BaseCode code, String path) {
        return new ResponseMessage<>(code.getCode(), code.getMsg(), null, path);
    }

    public static <T> ResponseMessage<T> createByError(BaseCode code, String message, String path) {
        return new ResponseMessage<>(code.getCode(), message, null, path);
    }

    public static <T> ResponseMessage<T> createByError(int code, String message, String path) {
        return new ResponseMessage<>(code, message, null, path);
    }

    @Override
    public String toString() {
        return "ResponseMessage{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", path=" + path +
                '}';
    }
}
